[HEADER]
	VERSION		00.00.00.00
	DESCRIPTION Complex Lighting Example
	COPYRIGHT	Imagination Technologies Ltd.
[/HEADER]

[TEXTURES]
	FILE base 			base.pvr			LINEAR-LINEAR-LINEAR
[/TEXTURES]

[VERTEXSHADER]
	NAME myVertShader
	[GLSL_CODE]
		attribute highp vec3	myVertex, myNormal;
		attribute mediump vec2	myUV;
		uniform mediump mat4	myMVPMatrix, myModelView;
		uniform mediump mat3	myModelViewIT;
		varying mediump vec2	texCoord;
		varying mediump vec3	diffuse, specular;
		const mediump float		shininess = 16.0;
		const mediump float		cutoff = 0.975, exp = 100.0;
		const mediump vec3		LightPos = vec3(4.0, 4.0, 4.0);
		const mediump vec3		LightCol = vec3(1.0,1.0,1.0);
		
		//directional light function //
		// spFlg	flag using specular or not.
		// nrml		nrml vector in the eye coordinate.
		// ePos		vertex position in the eye coordinate.
		void DirectionalLight(in mediump int spFlg, in mediump vec3 nrml, in mediump vec3 ePos){
		// calculate the light direction vector.
		mediump vec3 lightDir = normalize(LightPos);
		// calculate the half vector between eye position and light position.
		mediump vec3 halfV = normalize(-ePos + LightPos);
		// calculate diffuse light intensity.
		mediump float dVP = max(dot(nrml,lightDir), 0.0);
		// calculate approximated specular light base intensity.
		mediump float dHV = max(dot(nrml,halfV),0.0);
		// if the diffuse is not zero and spFlg is On,
		// calculate specular light intensity with shininess,
		// or turn off the specular light.
		mediump float pf;
		if (dVP>.0 && spFlg==1) pf = pow(dHV, shininess);
		else     			     pf = 0.0;
		diffuse += dVP*LightCol;
		specular += pf*LightCol;
	}

	// main function //
	void main(void){
		// transform the normal vector from the model coordinate to the eye coordinate.
		mediump vec3 normal = normalize(myModelViewIT * myNormal);
		// calculate the vertex position in the eye coordinate.
		highp vec3 ePos = vec3(myModelView * vec4(myVertex,1.0));
		// initalize light intensity parameter.
		diffuse = vec3(0.0);
		specular = vec3(0.0);

		DirectionalLight(1, normal, ePos);

		// Transform position
		gl_Position = myMVPMatrix * vec4(myVertex,1.0);
		// Pass UV co-ordinates
		texCoord = myUV.st;
	}
	[/GLSL_CODE]
[/VERTEXSHADER]

[FRAGMENTSHADER]
	NAME myFragShader
	[GLSL_CODE]
		uniform sampler2D 		sampler2d;
		varying mediump vec2	texCoord;
		varying mediump vec3	diffuse;
		varying mediump vec3	specular;
		void main (void)
		{
			mediump vec3 texColour  = vec3(texture2D(sampler2d, texCoord));
			mediump vec3 colour = (texColour * diffuse) + specular;
			gl_FragColor = vec4(colour, 1.0);
		}
	[/GLSL_CODE]
[/FRAGMENTSHADER]

[EFFECT]
	NAME myEffect

	ATTRIBUTE	myVertex			POSITION
	ATTRIBUTE	myNormal			NORMAL
	ATTRIBUTE	myUV				UV
	UNIFORM		myMVPMatrix			WORLDVIEWPROJECTION
	UNIFORM		myModelView			WORLDVIEW
	UNIFORM		myModelViewIT		WORLDVIEWIT
	UNIFORM		sampler2d			TEXTURE0

	TEXTURE 0 base

	VERTEXSHADER myVertShader
	FRAGMENTSHADER myFragShader
[/EFFECT]
